package com.vancouver.knows.ui.fragment.homePage;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.vancouver.knows.R;
import com.vancouver.knows.business.bean.HomeLiveFeedInfo;
import com.vancouver.knows.ui.activity.citydiscount.CityDiscountDetailActivity;
import com.vancouver.knows.ui.activity.citynews.CityNewsDetailActivity;
import com.vancouver.knows.ui.activity.userpost.UserPostDetailActivity;
import com.vancouver.knows.util.AppUtils;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by llf on 2016/6/12.
 */
public class LiveFeedViewFlipper {

    private Context mContext;
    private LayoutInflater mInflater;
    private View mView;

    private int count = 0;

    @Bind(R.id.image)
    ImageView mImageTitle;
    @Bind(R.id.content_title)
    TextView mContentTitle;
    @Bind(R.id.reply)
    TextView mReply;
    @Bind(R.id.layout_content)
    RelativeLayout layoutContent;

    public LiveFeedViewFlipper(Context context) {
        mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);;
        mView = mInflater.inflate(R.layout.homepage_live_feed, null);
        ButterKnife.bind(this, mView);
    }

    public void initValueForOnlyOne(HomeLiveFeedInfo listInfo){
        setListener(listInfo);
        setLiveFeedText(listInfo,  0);
    }

    public void initValueForMore(HomeLiveFeedInfo listInfo){
        setListener(listInfo);
        setLiveFeedText(listInfo,  count);
    }

    private void setLiveFeedText(HomeLiveFeedInfo listInfo, int pos){

        if (listInfo.getActivityType().equals("post")) {
            mContentTitle.setText(getStyleText(listInfo.getCommentPostTitle(), listInfo.getCommentAuthor()+" "
                    , AppUtils.getDateStr(mContext, listInfo.getCommentDateGmt()) + mContext.getString(R.string.new_post)+" "));
        } else if (listInfo.getActivityType().equals("image")) {
            mContentTitle.setText(getStyleText(listInfo.getCommentPostTitle(), listInfo.getCommentAuthor()+" "
                    , AppUtils.getDateStr(mContext, listInfo.getCommentDateGmt()) + mContext.getString(R.string.new_business_image)+" "));
        } else {
            mContentTitle.setText(getStyleText(listInfo.getCommentPostTitle(), listInfo.getCommentAuthor()+" "
                    , AppUtils.getDateStr(mContext, listInfo.getCommentDateGmt()) + mContext.getString(R.string.reviews_of)+" "));
        }
        mReply.setText(listInfo.getCommentContent());
        setImage(listInfo, mImageTitle);
    }


    private void setImage(HomeLiveFeedInfo info, ImageView image){
        Picasso.with(mContext).load(info.getCommentAuthorAvatar())//.placeholder(R.drawable.marker25)
                .resizeDimen(R.dimen.homepage_live_feed_width, R.dimen.homepage_live_feed_hight).centerCrop()
                .into(image);
    }

    private void setListener(HomeLiveFeedInfo info){
        layoutContent.setOnClickListener(new ReplyOnClickListener(info));
    }

    class ReplyOnClickListener implements OnClickListener {
        HomeLiveFeedInfo mListInfo = null;
        ReplyOnClickListener(HomeLiveFeedInfo info) {
            mListInfo = info;
        }

        @Override
        public void onClick(View v) {
            if (mListInfo != null) {
                String postType = mListInfo.getCommentPostType();
                if ("post".equals(postType)) {
                    Intent intent = new Intent(mContext, CityNewsDetailActivity.class);
                    intent.putExtra("postId", mListInfo.getId());
                    mContext.startActivity(intent);
                    //  } else if ("event".equals(postType)) {
                    //  cla = EventDetailActivity.class;
                } else if ("business".equals(postType)) {
                    Intent intent = new Intent(mContext, CityDiscountDetailActivity.class);
                    intent.putExtra("postId", mListInfo.getId());
                    mContext.startActivity(intent);
                } else if ("userpost".equals(postType)) {
                    Intent intent = new Intent(mContext, UserPostDetailActivity.class);
                    intent.putExtra(UserPostDetailActivity.POST_ID_EXTRA, mListInfo.getId());
                    mContext.startActivity(intent);
                }
            }
        }
    }

    private Spanned getStyleText(String content, String authorName, String time) {
        return Html.fromHtml("<font color=\"#F05326\">" + authorName + "</font>" + time + "<b>" + content + "</b>");
    }

    public View getLiveFeedView() {
        return mView;
    }

}

